import ajax from '../utils/ajax';
import $ from '../jquery/jquery.min.js'
const pageData = {
    currentPage: 1,//当前页
    pageSize: 7,//每页显示多少
    pages: 0,//总页数
    total: 0,//总条数
    now: 0//目前在第几条
}
renderPack();
renderPackPages();
renderPackList();

//新增仓库中的返回
$(".newPack").on("click", "#back", function () {
    $(".target").addClass("active");
    $(".packAdmin").removeClass("active");
})
//新增仓库
$(".newPack").on("click", "#save", function () {
    const packName = $(".newPack .packName").val();
    const packHome = $(".newPack .packHome").val();
    const packMan = $(".newPack .packMan").val();
    const packTel = $(".newPack .packTel").val();
    const packEmail = $(".newPack .packEmail").val();
    const packAddress = $(".newPack .packAddress").val();
    const packNote = $(".newPack .packNote").val();
    const packState = false;
    addPack(packName, packHome, packMan, packTel, packEmail, packAddress, packNote, packState);
    pageData.currentPage = 1;
    renderPack();
    renderPackPages();
})
//修改仓库
$(".packAdmin").on("click", ".changePack", async function () {
    $("#packChange").removeClass("active");
    const _id = $(this).data("id");
    const { result } = await findPack(_id);
    result.map((item, index) => {
        $(".changeName").val(item.packName);
        $(".changeMan").val(item.packMan);
        $(".changeTel").val(item.packTel);
        $(".changeEmail").val(item.packEmail);
        $(".changeAddress").val(item.packAddress);
        $(".changeNote").val(item.packNote);
        $("#changePack").attr("index", item._id);
        const options = $(".options");
        for (let i = 0; i < options.length; i++) {
            if (options[i].value == item.packHome) {
                options[i].setAttribute("selected", true);
                i = options.length;
            }
        }
    }
    )
})
//确认修改仓库
$("#packChange").on("click", ".change", async function () {
    changePack();
})
//删除
$(".packAdmin").on("click", ".delPack", async function () {
    const result = confirm("确认删除？");
    if (result) {
        const _id = $(this).data("id");
        delPack(_id);
        pageData.currentPage = 1;
        renderPack();
        renderPackPages();
    }
})
//切换启动
$(".packAdmin").on("click", ".changeState", async function () {
    const text = $(this).text();
    const result = confirm(`是否${text}仓库？`);
    if (text == "启用") {
        var packState = true;
    } else {
        var packState = false;
    }
    if (result) {
        const _id = $(this).data("id");
        startPack(_id, text, packState);
        renderPack();
    }

})
//搜索
$(".searchPack").on("blur", ".searchInput", function () {
    renderPack();
})
//切换每页显示
$(".packSelect").change(function () {
    const text = $(this).val();
    pageData.pageSize = text;
    renderPack();
    renderPackPages();
})
//上一页
$("#packLast").click(function () {
    if (pageData.currentPage > 1) {
        pageData.currentPage--;
        renderPack();
        renderPackPages();
    }

})
//下一页
$("#packNext").click(function () {
    if (pageData.currentPage < pageData.pages) {
        pageData.currentPage++;
        renderPack();
        renderPackPages();
    }
})
//点击切换某一页
$("#packSpan").on("click", ".spans", function () {
    const text = $(this).text();
    pageData.currentPage = text;
    renderPack();
    renderPackPages();
})
//点击跳转到某一页
$("#jumpPack").blur(function () {
    const val = $("#jumpPack").val();
    // console.log(val,pageData.currentPage);
    if (val <= pageData.pages) {
        pageData.currentPage = val;
        renderPack();
        renderPackPages();
    }
})



//功能区

//新增仓库
function addPack(packName, packHome, packMan, packTel, packEmail, packAddress, packNote, packState) {
    ajax({
        url: '/pack',
        type: 'POST',
        data: {
            packName, packHome,
            packMan, packTel,
            packEmail, packAddress,
            packNote, packState
        },
        success(res) {
            if (res.code == 200) {
                alert("添加成功");
                $(".packName").val('');
                $(".packHome").val('');
                $(".packMan").val('');
                $(".packTel").val('');
                $(".packEmail").val('');
                $(".packAddress").val('');
                $(".packNote").val('');
                renderPack();
            } else {
                alert("添加失败");
            }
        }
    })
}
//获取总仓库
function getPack() {
    const value = $(".searchInput").val();
    return new Promise((resolve, reject) => {
        ajax({
            url: '/pack/getPack',
            type: 'GET',
            data: {
                value,
                currentPage: pageData.currentPage,
                pageSize: pageData.pageSize
            },
            success(res) {
                resolve(res)
            }
        })
    })

}
//渲染仓库到页面
async function renderPack() {
    const { result, total, pages } = await getPack();
    pageData.pages = pages;
    pageData.total = total;
    const html = result.map((item, index) =>
        `<tr>
        <td class='packIndex'>${index + 1}</td>
        <td class='packName'>${item.packName}</td>
        <td class='packHome'>${item.packHome}</td>
        <td class='packState'>${item.packState ? "启用" : "禁用"}</td>
        <td class='packMan'>${item.packMan}</td>
        <td class='packTel'>${item.packTel}</td>
        <td class='packEmail'>${item.packEmail}</td>
        <td class='packAddress'>${item.packAddress}</td>
        <td><span class='changePack' data-id='${item._id}'>修改</span> <span class='changeState' data-id='${item._id}'>${item.packState ? "禁用" : "启用"}</span> <span class='delPack' data-id='${item._id}'>删除</span></td>
    </tr>`
    ).join("");
    $(".packAdmin tbody").html(html);



}
//通过ID查找仓库
function findPack(_id) {
    return new Promise((resolve, reject) =>
        ajax({
            url: `/pack/${_id}`,
            type: 'GET',
            success(res) {
                resolve(res);
            }
        })
    )
}
//修改仓库
function changePack() {
    const packName = $("#packChange .changeName").val();
    const packHome = $("#packChange .changeHome").val();
    const packMan = $("#packChange .changeMan").val();
    const packTel = $("#packChange .changeTel").val();
    const packEmail = $("#packChange .changeEmail").val();
    const packAddress = $("#packChange .changeAddress").val();
    const packNote = $("#packChange .changeNote").val();
    const _id = $('#changePack').attr("index");
    ajax({
        url: '/pack/changePack',
        type: 'post',
        data: {
            packName, packHome,
            packMan, packTel,
            packEmail, packAddress,
            packNote, _id
        },
        success(res) {
            console.log(res);
            if (res.code == 200) {
                alert("修改成功");
                $("#packChange .changeName").val('');
                $("#packChange .changeHome").val('');
                $("#packChange .changeMan").val('');
                $("#packChange .changeTel").val('');
                $("#packChange .changeEmail").val('');
                $("#packChange .changeAddress").val('');
                $("#packChange .changeNote").val('');
                renderPack();
            } else {
                alert("修改失败");
            }
        }
    })
}
//删除仓库
function delPack(_id) {
    ajax({
        url: '/pack',
        type: 'DELETE',
        data: {
            _id
        },
        success(res) {
            if (res.code == 200) {
                alert("删除成功");
                renderPack();
            } else {
                alert("删除失败");
            }
        }
    })
}
//启动或禁用仓库
function startPack(_id, text, packState) {
    ajax({
        url: '/pack/startPack',
        type: 'get',
        data: {
            _id, packState
        },
        success(res) {
            if (res.code == 200) {
                alert(`${text}仓库成功`);
                renderPack();
            } else {
                alert(`${text}仓库失败`);
                renderPack();
            }
        }
    })
}
//渲染页数
async function renderPackPages() {
    const { result, total, pages } = await getPack();
    pageData.pages = pages;
    pageData.total = total;
    var html = "";
    for (let i = 1; i <= pages; i++) {
        html += `<span class='spans'>${i}</span>`
    }
    $("#packSpan").html(html);

    const box = $("#packSpan .spans");
    for (let i = 0; i < box.length; i++) {
        if (box[i].innerText == pageData.currentPage) {
            box[i].style.backgroundColor = "#409EFF";
            box[i].style.color = "white";
        }
    }
}
//渲染仓库列表
async function renderPackList() {
    const { result, total, pages } = await getPack();
    const html = result.map((item, index) => {
        if (item.packState) {
            return `<option index="${item._id}">${item.packName}</option>
            `
        }
    }).join("");
    if (html) {
        $("#packSelect").html(html);
    }

}

